<template>
  <div>
    <el-form ref="skuForm" :model="skuForm" label-width="100px">
      <el-form-item label="SPU名称">
        {{ spu.spuName }}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input v-model="skuForm.skuName" placeholder="SKU名称"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          type="number"
          v-model="skuForm.price"
          placeholder="价格(元)"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input
          type="number"
          v-model="skuForm.weight"
          placeholder="重量(千克)"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          v-model="skuForm.skuDesc"
          placeholder="规格描述"
          rows="4"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="100px">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrList"
            :key="attr.id"
          >
            <el-select v-model="attr.attrIdandValueId" placeholder="请选择">
              <el-option
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="100px">
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select
              v-model="saleAttr.saleAttrIdandValueId"
              placeholder="请选择"
            >
              <el-option
                :label="saleAttrVal.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrVal.id}`"
                v-for="saleAttrVal in saleAttr.spuSaleAttrValueList"
                :key="saleAttrVal.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          border
          ref="multipleTable"
          :data="spuImageList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="{ row, $index }">
              <img :src="row.imgUrl" alt="" width="100px" height="80px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称"> </el-table-column>
          <el-table-column label="操作" show-overflow-tooltip>
            <template slot-scope="{ row, $index }">
              <el-button
                type="primary"
                size="mini"
                v-if="row.isDefault === '0'"
                @click="changeDefault(row)"
                >设为默认</el-button
              >
              <el-tag type="success" v-else>默认</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveSku">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "skuForm",
  data() {
    return {
      spu: {},
      spuImageList: [],
      skuImageList: [],
      attrList: [],
      spuSaleAttrList: [],
      dialogTableVisible: false,
      skuForm: {
        category3Id: 0,
        price: 0,
        spuId: 0,
        tmId: 0,
        weight: 0,
        skuDefaultImg: "",
        skuDesc: "",
        skuName: "",
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   attrName: "string",
          //   id: 0,
          //   skuId: 0,
          //   valueId: 0,
          //   valueName: "string",
          // },
        ],

        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],

        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],
      },
    };
  },
  methods: {
    async getInitSkuData(row, category1Id, category2Id) {
      this.spu = row;
      let result1 = this.$API.sku.getSpuImageList(row.id);
      let result2 = this.$API.sku.getSpuSaleAttrList(row.id);
      let result3 = this.$API.attr.getAttrInfoList(
        category1Id,
        category2Id,
        row.category3Id
      );
      let result = await Promise.all([result1, result2, result3]);
      let spuImageList = result[0].data;
      spuImageList.forEach((item) => {
        item.isDefault = "0";
      });
      this.spuImageList = spuImageList;
      this.attrList = result[2].data;
      this.spuSaleAttrList = result[1].data;
    },
    handleSelectionChange(val) {
      this.skuImageList = val;
    },
    changeDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = "0";
      });
      row.isDefault = "1";
      this.skuForm.skuDefaultImg = row.imgUrl
    },
    async saveSku() {
      let { skuImageList, attrList, spuSaleAttrList } = this;
      this.skuForm.tmId = this.spu.tmId
      this.skuForm.category3Id = this.spu.category3Id
      this.skuForm.spuId = this.spu.id
      // id:19
      // imgName:"19b35dc3ebe1579e.jpg"
      // imgUrl:"http://47.93.148.192:8080/group1/M00/00/02/rBHu8l-sklWAQwdLAACW7swImE4752.jpg"
      // isDefault:"0"
      // spuId:4
      this.skuForm.skuImageList = skuImageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
      this.skuForm.skuAttrValueList = attrList.reduce((prev, item) => {
        if (item.attrIdandValueId) {
          let [attrId, valueId] = item.attrIdandValueId.split(":");
          let obj = {
            attrId,
            valueId,
          };
          prev.push(obj);
        }
        return prev;
      }, []);
      this.skuForm.skuSaleAttrValueList = spuSaleAttrList.reduce((prev,item) => {
        if(item.saleAttrIdandValueId) {
          let [saleAttrId,saleAttrValueId] = item.saleAttrIdandValueId.split(':')
          let obj = {
            saleAttrId,saleAttrValueId
          }
          prev.push(obj)
        }
        return prev
      },[])
      let skuForm = this.skuForm
      try {
       let result = await this.$API.sku.addUpdate(skuForm)
       this.$message.success('添加sku成功')
       this.$emit("update:visible", false);
       this.resetData();
      }catch(e) {
        this.$message.error('添加sku失败'+e.message)
      }
    },
    cancel() {
      this.$emit("update:visible", false);
      this.resetData();
    },
    resetData() {
      Object.assign(this._data, this.$options.data());
    },
  },
};
</script>

<style lang="less" scoped>
</style>